<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>用代理实现单例</title>
	</head>
	<body>

	<script>
		// 一个普通的创建节点类
		var CreateDiv = function(html){
			this.html = html;
			this.init();
		};
		CreateDiv.prototype.init = function(){
			var div = document.createElement('div');
			div.innerHTML = this.html;
			document.body.appendChild(div);
		};

		// 引入代理类，把负责单例的逻辑移到了代码类中
		var ProxySingleCreateDiv = (function(){
			var instance;
			return function(html){
				if(!instance){
					instance = new CreateDiv(html);
				}
				return instance;
			}
		})();

		var a = new ProxySingleCreateDiv('sven1');
		var b = new ProxySingleCreateDiv('sven2');

		alert(a === b);

		// 本例也是缓存代理的应用之一
	</script>
	</body>
</html>
